package com.example.administrator.xiaobo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.fragment.LiveFragment;
import com.example.administrator.xiaobo.fragment.MineFragment;

import java.lang.reflect.InvocationTargetException;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private ImageView imageView;
    private FrameLayout frameLayout;
    private RadioGroup radioGroup;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Fragment currentFragment;

    private long duration;//旋转图标持续时间
    private Random random;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x100) {//下方图片需要更改
                int i = random.nextInt(mip.length);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setImageResource(mip[i]);
                handler.sendEmptyMessageDelayed(0x100,10000);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initAnim();
        radioGroup.setOnCheckedChangeListener(this);
    }

    //设置当前显示的界面
    public void showFragment(String tag, Class<? extends Fragment> className) {

        fragmentTransaction = fragmentManager.beginTransaction();
        if (currentFragment != null) {
             fragmentTransaction.hide(currentFragment);
        }
        currentFragment = fragmentManager.findFragmentByTag(tag);
        if (currentFragment != null) {
//            Log.e("dddd", "showFragment: "+"执行了");
            fragmentTransaction.show(currentFragment);
        } else {
            try {
                currentFragment = className.getConstructor().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            fragmentTransaction.add(R.id.parent, currentFragment, tag);
        }
        fragmentTransaction.commit();
    }
    private void initAnim() {
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.my_rotate);
        animation.setInterpolator(new LinearInterpolator());//匀速旋转
        imageView.startAnimation(animation);
        duration = Long.parseLong(getResources().getString(R.string.duration));
        handler.sendEmptyMessageDelayed(0x100,duration);
    }

    private void init() {
        imageView = (ImageView) findViewById(R.id.iv);
        random = new Random();//更新图片的随机数
        frameLayout = (FrameLayout) findViewById(R.id.parent);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        fragmentManager = getSupportFragmentManager();
        radioGroup.getChildAt(0).performClick();
        showFragment(LiveFragment.TAG, LiveFragment.class);
    }
    private int mip[] = {R.mipmap.notify_female_anchor_default_icon1,R.mipmap.notify_female_anchor_default_icon2,
            R.mipmap.notify_female_anchor_default_icon3,R.mipmap.notify_female_anchor_default_icon4,
            R.mipmap.notify_female_anchor_default_icon5,R.mipmap.notify_female_anchor_default_icon6,
            R.mipmap.notify_female_anchor_default_icon7,R.mipmap.notify_female_anchor_default_icon8,
            R.mipmap.notify_female_anchor_default_icon9,R.mipmap.notify_female_anchor_default_icon10,
            R.mipmap.notify_male_anchor_default_icon1,R.mipmap.notify_male_anchor_default_icon2,
            R.mipmap.notify_male_anchor_default_icon3,R.mipmap.notify_male_anchor_default_icon4,
            R.mipmap.notify_male_anchor_default_icon5,R.mipmap.notify_male_anchor_default_icon6,
            R.mipmap.notify_male_anchor_default_icon7,R.mipmap.notify_male_anchor_default_icon8,
            R.mipmap.notify_male_anchor_default_icon9,R.mipmap.notify_male_anchor_default_icon10,
    };//默认的图片

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb1:
                showFragment(LiveFragment.TAG,LiveFragment.class);
                break;
            case R.id.rb2:
             showFragment(MineFragment.TAG,MineFragment.class);
                break;
        }
    }
}
